import {IAccount, Account} from './Account';

const enum AccountStateLevel {
  Normal,
  Overdraft,
  Restricted
}

abstract class AccountState implements IAccount {
  name:string;
  account: Account;
  level:number;
  constructor(account: Account) {
    this.account = account;
  }
  deposit(amount: number): void {
    this.account.setBalance(amount + this.account.getBalance());
  }
  abstract withdraw(amount: number): void;
  abstract calcInterest(): number;
}

/**普通状态*/
class NormalState extends AccountState {
  name:string = '普通';
  level:number = AccountStateLevel.Normal;
  withdraw(amount: number): void {
    this.account.setBalance(this.account.getBalance() - amount);
  }
  calcInterest(): number {
    return 0;
  }
}

/**透支状态*/
class OverdraftState extends AccountState {
  name:string = '透支';
  level:number = AccountStateLevel.Overdraft;
  withdraw(amount: number): void {
    this.account.setBalance(this.account.getBalance() - amount);
  }
  calcInterest(): number {
    return 0.1;
  }
}

class RestrictedState extends AccountState {
  name:string = '受限';
  level:number = AccountStateLevel.Restricted;
  withdraw(amount: number): void {
    throw new Error('账户被限制，取款受限');
  }
  calcInterest(): number {
    return 0.1;
  }
}

export {
  AccountState,
  NormalState,
  OverdraftState,
  RestrictedState,
  AccountStateLevel
};